package com.buer.storage.business.service.impl;

import com.buer.storage.business.dao.AorderDao;
import com.buer.storage.business.domain.Aorderdetails;
import com.buer.storage.business.domain.Stock;
import com.buer.storage.business.dto.AorderDto;
import com.buer.storage.business.dto.AorderSelectDto;
import com.buer.storage.business.dto.AorderStorageDto;
import com.buer.storage.business.dto.AorderdetailsDto;
import com.buer.storage.business.service.AorderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AorderServiceImpl implements AorderService {
    @Resource
    private AorderDao aorderDao;

    @Override
    public int insert(AorderStorageDto aorderStorageDto) {
        aorderStorageDto.setAllotStatus(0);
        int result = aorderDao.insertAorder(aorderStorageDto);
        for (int i = 0; i < aorderStorageDto.getAorderPutProds().length; i++) {
            Aorderdetails aorderPutProd = aorderStorageDto.getAorderPutProds()[i];
            aorderPutProd.setAorderAllotid(aorderStorageDto.getAllotId());
        }
        aorderDao.insertAorderdetails(aorderStorageDto.getAorderPutProds());

        return result;
    }

    //根据订单表id查询入库信息
    @Override
    public List<AorderSelectDto> findAorderAllById (int allotId){
        List<AorderSelectDto> aorderAll = aorderDao.findAorderAll(allotId);
        if (aorderAll.size() > 0) {
            for (int i = 0; i < aorderAll.size(); i++) {
                List<AorderdetailsDto> all = aorderDao.findAorderdetailsAllById(aorderAll.get(i).getAllotId());
                if (all.size() > 0) {
                    aorderAll.get(i).setAorderPutProds(all);
                }
            }
        }

        return aorderAll;
    }


    //查询所有调拨表信息
    @Override
    public List<AorderDto> findAorder() {
        List<AorderDto> aorderAll = aorderDao.findAorder();
        return aorderAll;
    }

    //根据订单表id删除入库信息
    @Transactional
    @Override
    public int deleteAorderById(int allotId) {
        int deleteAorderById = aorderDao.deleteAorderById(allotId);
        return deleteAorderById;
    }

    //更新商品id
    @Transactional
    @Override
    public int updateAorderdetails(AorderStorageDto aorderStorageDto) {
        // 第一步先删除,此处需要前段讲putId的值传入过来
        Integer allotId = aorderStorageDto.getAllotId();
        aorderDao.deleteAorderByPutId(allotId);
        aorderDao.deleteAorderDetailsByPutId(allotId);
        // 第二部再插入
        aorderStorageDto.setAllotStatus(0);
        int result = aorderDao.insertAorder(aorderStorageDto);
        for (int i = 0; i < aorderStorageDto.getAorderPutProds().length; i++) {
            Aorderdetails aorderPutProd = aorderStorageDto.getAorderPutProds()[i];
            aorderPutProd.setAorderAllotid(aorderStorageDto.getAllotId());
        }
        aorderDao.insertAorderdetails(aorderStorageDto.getAorderPutProds());
        return result;
    }

    //调拨表增加
    @Transactional
    @Override
    public int submitAddBuerStock(Integer allotId) {
        AorderdetailsDto[] putProds = aorderDao.findAordertailsAll(allotId);
        aorderDao.updateAorderStatus(allotId);
        int result = 0;
        for (int i = 0; i < putProds.length; i++) {
            result = 1;
            AorderdetailsDto putProd = putProds[i];

            //先查找prodArea,prodDepart,prodId
            Stock stock = aorderDao.queryBuerStockByParam(putProd);

            //更新累加
            Integer prodNum = putProd.getProdNum();
            System.out.println(prodNum);
            Integer newProdNum = stock.getProdStocknum() - prodNum;
            System.out.println(newProdNum);
            Integer stockId = stock.getStockId();
            stock.setProdStocknum(newProdNum);
            if (newProdNum == 0) {
                aorderDao.deleteStock(stockId);
            } else {
                aorderDao.updateAddSumStock(stock);

            }
        }

        return result;
    }
}
